package org.dtrd.modules.evaluation.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.modules.base.service.BaseCommonService;
import org.dtrd.modules.evaluation.entity.bean.DiseaseEvaluationInfo;
import org.dtrd.modules.evaluation.entity.dto.DiseaseDTO;
import org.dtrd.modules.evaluation.entity.po.DtrdEntEvaluationRdNephropathy;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdNephropathyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.*;

/**
 * @Author: QR
 * @Date: 2021/9/28-13:10
 */
@Api(tags = "量表评估")
@RestController
public class NephropathyController extends BaseController {

    @Autowired
    private IDtrdEntEvaluationRdNephropathyService nephropathyService;
    @Autowired
    private BaseCommonService baseCommonService;

    @ApiOperation("添加糖尿病肾病评估")
    @PostMapping(value = {"/105/22/1052201"})
    public Result<?> saveNephropathyInfo(@RequestBody DiseaseDTO dto) {
        // 参数校验
        if (dto.getIsDiagnose() == null || dto.getSymptom() == null) {
            return Result.error(STATE_PARAMS_MISSING.getCode(), STATE_PARAMS_MISSING.getMsg());
        }
        // 校验没有填值传入的默认值
        Optional<Integer> any = Arrays.stream(dto.getSymptom())
                .filter(answer -> answer == -1)
                .findAny();
        if (any.isPresent()){
            return error(STATE_PARAMS_MISSING);
        }
        if (dto.getIsDiagnose() <= 0) {
            return Result.error(STATE_PARAMS_ERROR.getCode(), STATE_PARAMS_ERROR.getMsg());
        }
        DiseaseEvaluationInfo info = nephropathyService.saveNephropathyInfo(dto);
        String logContent = String.format("添加糖尿病肾病评估，添加用户id：%d", dto.getPatientId());
        baseCommonService.addLog(logContent, OPERATE_TYPE_ADD);
        return Result.OK(info);
    }

    @ApiOperation("查询最近一次的糖尿病肾病评估")
    @GetMapping("/105/22/1052202")
    public Result<?> getNephropathyInfoLatest(@PathParam("patientId") Integer patientId) {
        if (isIntParamInvalid(patientId)){
            return error(STATE_PARAMS_MISSING);
        }
        DiseaseEvaluationInfo info = nephropathyService.getNephropathyInfoLatest(patientId);
        String logContent = String.format("查询最近一次的糖尿病肾病评估，查询用户id：%d", patientId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(info);
    }

    @ApiOperation("查询糖尿病肾病评估列表")
    @GetMapping("/105/22/1052203")
    public Result<?> getNephropathyInfoList(@RequestParam("patientId") Integer patientId){
        if (isIntParamInvalid(patientId)){
            return error(STATE_PARAMS_MISSING);
        }
        LambdaQueryWrapper<DtrdEntEvaluationRdNephropathy> queryWrapper = Wrappers.lambdaQuery(DtrdEntEvaluationRdNephropathy.class)
                .eq(DtrdEntEvaluationRdNephropathy::getPatientId, patientId);
        List<DtrdEntEvaluationRdNephropathy> poList = nephropathyService.list(queryWrapper);
        List<DiseaseEvaluationInfo> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()){
            dtoList = poList.stream()
                    .map(po -> new DiseaseEvaluationInfo().parseFromPo(po))
                    .collect(Collectors.toList());
        }
        String logContent = String.format("查询糖尿病肾病评估列表，查询用户id：%d", patientId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(dtoList);
    }
    @ApiOperation("删除糖尿病肾病评估列表")
    @DeleteMapping("/105/22/1052204")
    public Result<?> removeNephropathyInfoById(@RequestParam("dataId") Integer dataId){
        if (isIntParamInvalid(dataId)){
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = nephropathyService.removeById(dataId);
        String logContent = String.format("删除糖尿病肾病评估列表，删除记录id：%d", dataId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_DELETE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
